/**
 * 处理Todo事物的action
 */
package com.zhao.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONString;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.zhao.entity.Thing;
import com.zhao.entity.User;
import com.zhao.service.ThingService;
import com.zhao.utils.PageUtils;
import com.zhao.utils.ReturnUtils;

public class ThingAction extends ActionSupport implements ServletRequestAware,ServletResponseAware{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private HttpServletResponse response;
	private HttpServletRequest request;
	private int pageNum;
	private int thingid;
	private String condition;
	
	@Autowired
	private ThingService thingService;
	/**
	 * 获取传入参数
	 */
	private Thing thing;
	
	/**
	 * 添加thing
	 */
	public void addThing() {
		User user = (User) request.getSession().getAttribute("user");
		Map<String, Object> map = new HashMap<String, Object>();
 		//添加Todo thing
		try {
			thingService.addThing(thing, user);
			map.put("status", 1);
			JSON json = JSONObject.fromObject(map);
			//向前台传递状态码
			ReturnUtils.responseJson(response, json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 查询该用户的thing
	 */
	public void getThings() {
		User user = (User) request.getSession().getAttribute("user");
		
		//查看当前页处理
		PageUtils pageUtils = new PageUtils(thingService.getTotalThing(user, condition), pageNum);
		
		//查询分页后的值
		List<Map<String, Object>> things = thingService.getThings(pageUtils, user, condition);
		
		//封装返回值
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("things", things);
		map.put("pageUtils", pageUtils);
		ReturnUtils.responseJson(response, JSONObject.fromObject(map));
	}
	
	/**
	 * 设定thing为完成状态
	 */
	public void finishThingSingle() {
		//设定完成状态
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			thingService.finishThingSingle(thingid);
			map.put("status", 1);
			JSON json = JSONObject.fromObject(map);
			//向前台传递状态码
			ReturnUtils.responseJson(response, json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public Thing getThing() {
		return thing;
	}

	public void setThing(Thing thing) {
		this.thing = thing;
	}
	
	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
		
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public int getThingid() {
		return thingid;
	}

	public void setThingid(int thingid) {
		this.thingid = thingid;
	}
	
	public String getCondition() {
		return condition;
	}

	public void setCondition(String condition) {
		this.condition = condition;
	}
}
